//
//  MyEmotionkeyBoard.m
//  WeChat
//
//  Created by ibokan on 16/2/24.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "YJEmotionkeyBoard.h"
#import "YJEmotiontabBar.h"
#import "YJEmotionListView.h"
#import "YJEmotion.h"
#import "YJGifEmotionListView.h"
#import "YJGifEmotionModel.h"
#import "YJEmotionTabBarBtn.h"
#import "YJUserDefaultTool.h"
#define TABBARH  40
@interface YJEmotionkeyBoard()
@property(nonatomic,strong)UIView *contentView;
@property(nonatomic,strong)YJEmotionListView *defaultListView;
@property(nonatomic,strong)YJGifEmotionListView  *gifEmotionListView;
@property(nonatomic,strong)YJGifEmotionListView  *collectionEmotionListView;

/**  缓存从本地读取的表情包 */
@property(nonatomic,strong)NSMutableDictionary  *emotionDict;
@end
@implementation YJEmotionkeyBoard
-(YJEmotionListView *)defaultListView
{
    if (_defaultListView == nil)
    {
        _defaultListView = [[YJEmotionListView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height - TABBARH)];
        NSString *path = [[NSBundle mainBundle]pathForResource:@"default/info.plist" ofType:nil];
        _defaultListView.emotions = [YJEmotion emotionsWith:[NSArray arrayWithContentsOfFile:path]];
    }
    return _defaultListView;
}

- (YJGifEmotionListView *)gifEmotionListView
{
    if (_gifEmotionListView == nil)
    {
        _gifEmotionListView = [[YJGifEmotionListView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height - TABBARH)];
    }
    return _gifEmotionListView;
}

- (YJGifEmotionListView *)collectionEmotionListView
{
    if (_collectionEmotionListView == nil)
    {
        _collectionEmotionListView = [[YJGifEmotionListView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height - TABBARH)];
        NSString *fileName = [NSString stringWithFormat:@"collectionEmotion_%@.archive",[YJUserDefaultTool readUserID]];
        NSString *filePath = LIBFILEPATH(fileName);
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        NSArray *emotions = [[YJGifEmotionModel gifEmotionsWith:array] mutableCopy];
        NSMutableArray *emotionModels = [NSMutableArray array];
        //+号开头
        YJGifEmotionModel *model = [[YJGifEmotionModel alloc] init];
        model.isAddBtn = YES;
        [emotionModels addObject:model];
        [emotionModels addObjectsFromArray:emotions];
        _collectionEmotionListView.emotions = emotionModels;
    }
    return _collectionEmotionListView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.emotionDict = [NSMutableDictionary dictionary];
        self.backgroundColor = [UIColor whiteColor];
        //底部工具条
        YJEmotiontabBar *emotiomTabBar = [[YJEmotiontabBar alloc]initWithFrame:CGRECT(0,216 - TABBARH,IPHONE_W,TABBARH)];
        YJWeakSelf
        emotiomTabBar.tabBarButtonBlock = ^(YJEmotionTabBarBtn *btn){
            [weakSelf tabBarButtonAction:btn];
        };
        [self addSubview:emotiomTabBar];
        
        //容纳表情的View
        self.contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.width, emotiomTabBar.y)];
        [self addSubview:self.contentView];
        //首先展示默认表情
        [self.contentView addSubview:self.defaultListView];
        //监听收藏表情的通知、收藏的表情首次下载好也是该通知
        [NOTIFICATIONCENTER addObserver:self selector:@selector(didAddEmotion) name:YJDidAddEmotionNotification object:nil];
    }
    return self;
}

-(void)tabBarButtonAction:(YJEmotionTabBarBtn *)btn
{
    //移除所有的子视图
    [self.contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    switch (btn.tag)
    {
        case 10://默认
        {
            [self.contentView addSubview:self.defaultListView];
        }
            break;
        case 11://收藏
        {
            [self.contentView addSubview:self.collectionEmotionListView];
        }
            break;
            default://从网络下载的表情包
        {
            if (self.emotionDict[btn.teamId])
                self.gifEmotionListView.emotions = self.emotionDict[btn.teamId];
            else
            {
                NSString *fileName = [NSString stringWithFormat:@"emotion_%@.archive",btn.teamId];
                NSArray *emotions = [NSKeyedUnarchiver unarchiveObjectWithFile:LIBFILEPATH(fileName)];
                self.emotionDict[btn.teamId] = [YJGifEmotionModel gifEmotionsWith:emotions];
                self.gifEmotionListView.emotions = self.emotionDict[btn.teamId];
            }
            [self.contentView addSubview:self.gifEmotionListView];
        }
            break;
    }
}

- (void)didAddEmotion
{
    NSString *fileName = [NSString stringWithFormat:@"collectionEmotion_%@.archive",[YJUserDefaultTool readUserID]];
    NSString *filePath = LIBFILEPATH(fileName);
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSArray *emotions = [[YJGifEmotionModel gifEmotionsWith:array] mutableCopy];
    NSMutableArray *emotionModels = [NSMutableArray array];
    YJGifEmotionModel *model = [[YJGifEmotionModel alloc] init];
    model.isAddBtn = YES;
    [emotionModels addObject:model];
    [emotionModels addObjectsFromArray:emotions];
    self.collectionEmotionListView.emotions = emotionModels;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{}

- (void)dealloc
{
    [NOTIFICATIONCENTER removeObserver:self];
}


@end
